# 统计文件中26个字母的数量
# 方法一
f = open("latex.txt", "r", encoding="utf-8")
d = {}
sc = 0
for line in f:
    # line = line.strip()
    # line = line.replace(" ", "")
    # if len(line) > 0:
    #     print(line)
    for i in range(len(line)):
        sc += 1
        for c in "abcdefghijklmnopqrstuvwxyz":
            if line[i] == c:
                d[c] = d.get(c, 0) + 1
# for i in sorted(d):
#     print(i, ":", d[i], end=" ")
print("共{}字符".format(sc), end=",")
items = list(d.items())
items.sort(key=lambda x: x[0], reverse=False)
for i in range(26):
    word, count = items[i]
    print("{}:{}".format(word, count), end=",")
print()

# 方法二
f = open("latex.txt")
cc = 0
d = {}
for i in range(26):
    d[chr(ord('a')+i)] = 0
for line in f:
    for c in line:
        d[c] = d.get(c, 0) + 1
        cc += 1
print("共{}字符".format(cc), end="")
for i in range(26):
    if d[chr(ord('a')+i)] != 0:
        print(",{}:{}".format(chr(ord('a')+i), d[chr(ord('a')+i)]), end="")


# # 计算文本所有行平均字符数
# f = open('latex.txt', 'r', encoding='utf-8')
# sum = 0
# ln = 0
# for line in f:
#     line = line.strip()
#     if len(line) > 0:
#         line = line.replace(" ", "")
#         sum = sum + len(line)
#         ln += 1
# print(round(sum/ln))




